package com.byb.server.service.impl;

import com.byb.server.service.EnsureScopeService;
import com.byb.server.domain.EnsureScope;
import com.byb.server.repository.EnsureScopeRepository;
import com.byb.server.repository.search.EnsureScopeSearchRepository;
import com.byb.server.service.dto.EnsureScopeDTO;
import com.byb.server.service.mapper.EnsureScopeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing EnsureScope.
 */
@Service
@Transactional
public class EnsureScopeServiceImpl implements EnsureScopeService{

    private final Logger log = LoggerFactory.getLogger(EnsureScopeServiceImpl.class);

    private final EnsureScopeRepository ensureScopeRepository;

    private final EnsureScopeMapper ensureScopeMapper;

    private final EnsureScopeSearchRepository ensureScopeSearchRepository;
    public EnsureScopeServiceImpl(EnsureScopeRepository ensureScopeRepository, EnsureScopeMapper ensureScopeMapper, EnsureScopeSearchRepository ensureScopeSearchRepository) {
        this.ensureScopeRepository = ensureScopeRepository;
        this.ensureScopeMapper = ensureScopeMapper;
        this.ensureScopeSearchRepository = ensureScopeSearchRepository;
    }

    /**
     * Save a ensureScope.
     *
     * @param ensureScopeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public EnsureScopeDTO save(EnsureScopeDTO ensureScopeDTO) {
        log.debug("Request to save EnsureScope : {}", ensureScopeDTO);
        EnsureScope ensureScope = ensureScopeMapper.toEntity(ensureScopeDTO);
        ensureScope = ensureScopeRepository.save(ensureScope);
        EnsureScopeDTO result = ensureScopeMapper.toDto(ensureScope);
       // ensureScopeSearchRepository.save(ensureScope);
        return result;
    }

    /**
     *  Get all the ensureScopes.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<EnsureScopeDTO> findAll(Pageable pageable) {
        log.debug("Request to get all EnsureScopes");
        return ensureScopeRepository.findAll(pageable)
            .map(ensureScopeMapper::toDto);
    }

    /**
     *  Get one ensureScope by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public EnsureScopeDTO findOne(Long id) {
        log.debug("Request to get EnsureScope : {}", id);
        EnsureScope ensureScope = ensureScopeRepository.findOne(id);
        return ensureScopeMapper.toDto(ensureScope);
    }

    /**
     *  Delete the  ensureScope by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete EnsureScope : {}", id);
        ensureScopeRepository.delete(id);
        //ensureScopeSearchRepository.delete(id);
    }

    /**
     * Search for the ensureScope corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<EnsureScopeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of EnsureScopes for query {}", query);
        Page<EnsureScope> result = ensureScopeSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(ensureScopeMapper::toDto);
    }
}
